package com.ruoyi.ruoyiqywechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.qy.domain.User;
import com.ruoyi.common.qy.exception.CrmebException;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ruoyiqywechat.domain.UserMembership;
import com.ruoyi.ruoyiqywechat.service.IUserMembershipService;
import com.ruoyi.ruoyiqywechat.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 小程序用户信息
 */
@Slf4j
@RestController
@RequestMapping("/wechatUser")
public class WechatUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private IUserMembershipService userMembershipService;
    /**
     * 获取个人信息
     */
    //(value = "获取个人信息")
    @GetMapping(value = "/info")
    public R<User> info(HttpServletRequest request){
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isBlank(token)) {
            throw new CrmebException("请先登录!");
        }
        String userId = JwtUtils.getUserId(token);
        User user = userService.getById(userId);
//        //判断当前会员是否重复，如果是则不允许重复会员充值
//        QueryWrapper<UserMembership> userMembershipQueryWrapper = new QueryWrapper<>();
//        userMembershipQueryWrapper.lambda().eq(UserMembership::getUserId,userId)
//                .eq(UserMembership::getStatus,1);
//        UserMembership one = userMembershipService.getOne(userMembershipQueryWrapper);
//        if(one != null){
//           user.setIsFenjian(true);
//           user.setFenjianTime(one.getStartTime());
//        }
        return R.ok(user);
    }
    /**
     * 修改个人信息
     */
    //(value = "修改个人信息")
    @PostMapping(value = "/modifyInfo")
    public R<User> modifyInfo(HttpServletRequest request,@RequestBody User user){
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isBlank(token)) {
            throw new CrmebException("请先登录!");
        }
        String userId = JwtUtils.getUserId(token);
        User serviceById = userService.getById(userId);
        serviceById.setNickname(user.getNickname());
        serviceById.setAvatar(user.getAvatar());
        userService.updateById(serviceById);
        return R.ok(serviceById);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @GetMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
        }
        return R.ok();
    }


    /**
     * 绑定分享用户ID
     * @param shareId 分享用户ID
     * @return
     */
    @GetMapping("bindShareId")
    public R<Boolean> bindShareId(HttpServletRequest request, @RequestParam("shareId") Integer shareId){
        return R.ok(userService.bindShareId(request,shareId));
    }





    /**
     * 查询分享的用户
     * @return
     */
    @GetMapping("querySpIdUserList")
    public R<PageInfo<User>> querySpIdUserList(HttpServletRequest request, @RequestParam("pageNum") Integer pageNum
            , @RequestParam("pageSize") Integer pageSize){
        return R.ok(userService.querySpIdUserList(request,pageNum,pageSize));
    }
}
